package untiring.services;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sun.imageio.plugins.common.ImageUtil;

import untiring.helper.FileUploadUtil;
import untiring.helper.ServletImageUtil;
import untiring.interfaces.GalleryDAO;
import untiring.interfaces.GalleryService;
import untiring.models.GalleryVO;


@Service("GalleryService") 
public class GalleryServiceImpl implements GalleryService{
	
	@Autowired
	private GalleryDAO galleryDAO;
	@Autowired
	private FileUploadUtil fileUpload;
	@Autowired
	private ServletImageUtil imageUtil;
	
	@Override
	public String newGallery(GalleryVO g) {
		String viewPage = "gallery/write";
		if(galleryDAO.insertGallery(g))
			viewPage = "redirect:/gallery/list.mvc";
		return viewPage;
	}

	@Override
	public List<GalleryVO> readGallery() {
		
		return galleryDAO.selectGallery();
	}

	@Override
	public GalleryVO readOneGallery(String gno) {
		return galleryDAO.selectOneGallery(gno);
	}

	@Override
	public String modifyGallery(GalleryVO g) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String removeGallery(String gno) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void manageImage(GalleryVO g) {
		fileUpload.makeUUID(g);
		try {
			fileUpload.processUpload(g);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	   public byte[] viewImage(GalleryVO g, HttpServletResponse res) throws Exception{
	      GalleryVO i = galleryDAO.selectImage(g);
	      String fname = i.getUuid() + i.getIno();
	      return imageUtil.processImage(fname, res);
	   }
	

	

	

}
